import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  ParseIntPipe,
  UseGuards,
  Query,
  ParseBoolPipe,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
import { MembershipService } from './membership.service';
import { CreateMembershipPackageDto } from './dto/create-membership-package.dto';
import { UpdateMembershipPackageDto } from './dto/update-membership-package.dto';
import { BatchUpdateStatusDto } from './dto/batch-update-status.dto';
import { MembershipPackage } from './entities/membership-package.entity';
import { JwtAuthGuard } from '../../../common/guards/jwt-auth.guard';
import { Public } from '../../../common/decorators/public.decorator';

/**
 * 会员套餐控制器
 */
@ApiTags('会员套餐')
@Controller('member/packages')
@UseGuards(JwtAuthGuard)
export class MembershipController {
  constructor(private readonly membershipService: MembershipService) {}

  @ApiOperation({ summary: '创建会员套餐' })
  @ApiResponse({ status: 201, description: '创建成功', type: MembershipPackage })
  @Post()
  async create(@Body() createDto: CreateMembershipPackageDto): Promise<MembershipPackage> {
    return await this.membershipService.create(createDto);
  }

  @ApiOperation({ summary: '获取所有启用的会员套餐' })
  @ApiResponse({ status: 200, description: '获取成功', type: [MembershipPackage] })
  @Public()
  @Get()
  async findAll(): Promise<MembershipPackage[]> {
    return await this.membershipService.findAll();
  }

  @ApiOperation({ summary: '管理员获取所有会员套餐' })
  @ApiResponse({ status: 200, description: '获取成功', type: [MembershipPackage] })
  @Get('admin')
  async findAllForAdmin(): Promise<MembershipPackage[]> {
    return await this.membershipService.findAllForAdmin();
  }

  @ApiOperation({ summary: '分页获取会员套餐' })
  @ApiResponse({ 
    status: 200, 
    description: '获取成功',
    schema: {
      type: 'object',
      properties: {
        data: { type: 'array', items: { $ref: '#/components/schemas/MembershipPackage' } },
        total: { type: 'number', description: '总数量' },
        page: { type: 'number', description: '当前页码' },
        limit: { type: 'number', description: '每页数量' },
        totalPages: { type: 'number', description: '总页数' }
      }
    }
  })
  @Get('page')
  async findWithPagination(
    @Query('page', ParseIntPipe) page: number = 1,
    @Query('limit', ParseIntPipe) limit: number = 10,
    @Query('includeInactive', ParseBoolPipe) includeInactive: boolean = false,
  ) {
    return await this.membershipService.findWithPagination(page, limit, includeInactive);
  }

  @ApiOperation({ summary: '根据ID获取会员套餐' })
  @ApiResponse({ status: 200, description: '获取成功', type: MembershipPackage })
  @Public()
  @Get(':id')
  async findOne(@Param('id', ParseIntPipe) id: number): Promise<MembershipPackage> {
    return await this.membershipService.findOne(id);
  }

  @ApiOperation({ summary: '更新会员套餐' })
  @ApiResponse({ status: 200, description: '更新成功', type: MembershipPackage })
  @Patch(':id')
  async update(
    @Param('id', ParseIntPipe) id: number,
    @Body() updateDto: UpdateMembershipPackageDto,
  ): Promise<MembershipPackage> {
    return await this.membershipService.update(id, updateDto);
  }

  @ApiOperation({ summary: '删除会员套餐' })
  @ApiResponse({ status: 200, description: '删除成功' })
  @Delete(':id')
  async remove(@Param('id', ParseIntPipe) id: number): Promise<{ message: string }> {
    await this.membershipService.remove(id);
    return { message: '会员套餐删除成功' };
  }

  @ApiOperation({ summary: '启用会员套餐' })
  @ApiResponse({ status: 200, description: '启用成功', type: MembershipPackage })
  @Patch(':id/enable')
  async enable(@Param('id', ParseIntPipe) id: number): Promise<MembershipPackage> {
    return await this.membershipService.enable(id);
  }

  @ApiOperation({ summary: '禁用会员套餐' })
  @ApiResponse({ status: 200, description: '禁用成功', type: MembershipPackage })
  @Patch(':id/disable')
  async disable(@Param('id', ParseIntPipe) id: number): Promise<MembershipPackage> {
    return await this.membershipService.disable(id);
  }

  @ApiOperation({ summary: '根据等级获取会员套餐' })
  @ApiResponse({ status: 200, description: '获取成功', type: [MembershipPackage] })
  @Public()
  @Get('level/:level')
  async findByLevel(@Param('level', ParseIntPipe) level: number): Promise<MembershipPackage[]> {
    return await this.membershipService.findByLevel(level);
  }

  @ApiOperation({ summary: '获取推荐套餐' })
  @ApiResponse({ status: 200, description: '获取成功', type: [MembershipPackage] })
  @Public()
  @Get('recommended')
  async findRecommended(): Promise<MembershipPackage[]> {
    return await this.membershipService.findRecommended();
  }

  @ApiOperation({ summary: '获取套餐统计信息（管理员）' })
  @ApiResponse({ 
    status: 200, 
    description: '获取成功',
    schema: {
      type: 'object',
      properties: {
        total: { type: 'number', description: '总套餐数' },
        active: { type: 'number', description: '启用套餐数' },
        inactive: { type: 'number', description: '禁用套餐数' },
        levels: {
          type: 'array',
          items: {
            type: 'object',
            properties: {
              level: { type: 'number', description: '等级' },
              count: { type: 'number', description: '数量' }
            }
          }
        }
      }
    }
  })
  @Get('admin/statistics')
  async getStatistics() {
    return await this.membershipService.getStatistics();
  }

  @ApiOperation({ summary: '搜索会员套餐' })
  @ApiResponse({ status: 200, description: '搜索成功', type: [MembershipPackage] })
  @Get('search')
  async search(
    @Query('keyword') keyword: string,
    @Query('includeInactive', ParseBoolPipe) includeInactive: boolean = false,
  ): Promise<MembershipPackage[]> {
    return await this.membershipService.search(keyword, includeInactive);
  }

  @ApiOperation({ summary: '批量更新套餐状态' })
  @ApiResponse({ status: 200, description: '更新成功' })
  @Patch('batch-status')
  async batchUpdateStatus(
    @Body() batchUpdateStatusDto: BatchUpdateStatusDto,
  ): Promise<{ message: string }> {
    await this.membershipService.batchUpdateStatus(
      batchUpdateStatusDto.ids, 
      batchUpdateStatusDto.isActive
    );
    return { message: '批量更新成功' };
  }
}